//
//  ZSTabBar.swift
//  ZSWeibo
//
//  Created by apple on 15/11/23.
//  Copyright © 2015年 apple. All rights reserved.
//

import UIKit

class ZSTabBar: UITabBar
{
    override func layoutSubviews()
    {
        super.layoutSubviews()
        //计算item的宽度
        let width = bounds.width / 5
        //创建frame
        let frame = CGRect(x: 0, y: 0, width: width, height: bounds.height)
        //用来记录是第一个view
        var index = 0
        //重新设置按钮位置(UITabBarButton)
        for view in subviews
        {
            //只有系统的UITabBarButton需要设置frame
            //&& !(view is UIButton)撰写按钮也是属于UIControl类型，那么这样判断就会导致tabBar上按钮的顺序混乱，所以需要再重新添加一个并且不是按钮的条件，这样，撰写按钮的frame才不会在这里设置
            if view is UIControl && !(view is UIButton)
            {
                //设置frame 
                view.frame = CGRectOffset(frame, CGFloat(index) * width, 0)
                index += index == 1 ? 2 : 1
            }
        }
        //设置撰写按钮的frame
        composeButton.frame = CGRectOffset(frame, width * 2, 0)
        
    }
    
    //懒加载 撰写按钮
    lazy var composeButton: UIButton = {
        //创建按钮
        let button = UIButton()
        
        //设置参数
        //设置按钮背景图片
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        //设置按钮图片
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        //将创建好的撰写按钮添加到tabbar里面
        self.addSubview(button)
        return button
        }()
    
    
    
    
}
